package fr.nekloth.android.hinode;

import uk.me.jstott.sun.Time;

import java.util.UUID;

/**
 *   SunAlarm Class
 *   
 *   Use to define a sun relative alarm, based on the location
 *   
 *   This class use the GPL Class developped by Jonathan Stott, and the
 *   following packages:
 *        - uk.me.jstott.coordconv
 *        - uk.me.jstott.sun
 *        - uk.me.jstott.util
 *        
 * @author Nekloth - nekloth@gmail.com
 * @version 0.1
 * 
**/
public class SunAlarm {

	/*
	 * Variables n�cessaires
	 */
	private int alarmHours;
	private int alarmMinutes;
	private boolean relative; // TRUE=BEFORE - FALSE=AFTER
	private Time reference;
	private Time alarmTime;
	private boolean activated;
	private int momentReference;
    private String ID;
    
	/*
	 * Constants
	 */
	public static int REFERENCE_MORNING_TWILIGHT = 0;
	public static int REFERENCE_SUNRISE          = 1;
	public static int REFERENCE_SUNSET           = 2;
	public static int REFERENCE_EVENING_TWILIGHT = 3;
    public static int BEFORE = 0;
    public static int AFTER  = 1;
    

    
    
	/*
	 * Constructeurs
	 */

	public SunAlarm(){
		this.relative = true;
		this.reference = new Time(0, 0, 0.0d);
		this.alarmTime = new Time(0,0, 0.0d);
		this.setAlarmHour(this.alarmTime.getHours());
		this.setAlarmMinutes(this.alarmTime.getMinutes());
		this.activated = false;
		this.momentReference = SunAlarm.REFERENCE_EVENING_TWILIGHT;
		this.ID = UUID.randomUUID().toString();
		updateAlarmTime();
	}
	/** *****************************************************
	 * Accesseurs GET
	 ***************************************************** **/

	public String getID() {
		return this.ID;
	}
	
	public String getAlarmTimeString(boolean format24){
		return "" + ((format24)?((this.alarmTime.getHours()<10)?"0":""):((this.alarmTime.getHours()-12<10)?"0":"")) + ((format24)?this.alarmTime.getHours():(this.alarmTime.getHours()>12)?this.alarmTime.getHours()-12:this.alarmTime.getHours()) + ":" + ((this.alarmTime.getMinutes()<10)?"0":"") + this.alarmTime.getMinutes() + ((format24)?"":(this.alarmTime.getHours()>12)?" pm":" am");
	}
	
	public Time getAlarmTime(){
		return this.alarmTime;
	}
	
	public String getAlarmTimeString(){
		return getAlarmTimeString(true);
	}

	public int getCondensedAlarmTime() {
    	return ((alarmTime.getHours() * 100) + alarmTime.getMinutes());
	}
	
	public String toString(){
		return "" + alarmTime.getHours() + ":" + alarmTime.getMinutes();
	}
	
	public boolean isActive() {
		return this.activated;
	}
	
	public boolean isBeforeReference() {
		return this.relative;
	}
	
	public boolean isAfterReference() {
		return !(this.relative);
	}

	public int getMomentReference() {
		return momentReference;
	}
	
	public Time getReferenceTime() {
		return this.reference;
	}
	
	public int getAlarmHour() {
		return this.alarmHours;
	}
	
	public int getAlarmMinute(){
		return this.alarmMinutes;
	}
	
	public boolean getRelative() {
		return this.relative;
	}
	
	/** *****************************************************
	 * Accesseurs SET
	 ***************************************************** **/

	public void setID(String theId) {
		this.ID = theId;
	}
	
	public void setAlarmHour(int hour) {
		this.alarmHours = hour;
		updateAlarmTime();
	}

	public void setAlarmMinutes(int minute) {
		this.alarmMinutes = minute;
		updateAlarmTime();
	}
	
	public void setAlarmTime(int hour, int minute) {
		setAlarmHour(hour);
		setAlarmMinutes(minute);
		updateAlarmTime();
	}
	
	public void activate() {
		this.activated = true;
		this.updateAlarmTime();
	}
	
	public void desactivate() {
		this.activated = false;
		this.updateAlarmTime();
	}

	public void toggleActivate() {
		if (this.isActive()) {
			this.desactivate();
		} else {
			this.activate();
		}
	}
	
	public void setIsBefore() {
		this.relative = true;
		this.updateAlarmTime();
	}
	
	public void setIsAfter() {
		this.relative = false;
		this.updateAlarmTime();
	}

	public void setMomentReference(int reference) {
		if ( reference <= 4 && reference >= 0 ) {
			this.momentReference = reference;
		} else {
			this.momentReference = 0;
		}
		this.updateAlarmTime();
	}
	
	public void setTimeReference(Time reference) {
		this.reference=reference;
		this.updateAlarmTime();
	}
	
	/** *****************************************************
	 * Others method
	 ***************************************************** **/

	/*
	 * Calculate ALARMTIME by taking REFERENCE +/- this.HOUR and this.MINUT
	 */
	public void updateAlarmTime (){
		int newHour, newMinut;
		
		if (relative) { //Before
			newMinut = reference.getMinutes() - alarmMinutes;
			newHour = reference.getHours() - alarmHours - ((newMinut<0)?1:0);
			newMinut = (newMinut<0)?newMinut+60:newMinut;
			newHour = (newHour<0)?newHour+24:newHour;
		} else {
			newMinut = reference.getMinutes() + alarmMinutes;
			newHour = reference.getHours() + alarmHours + ((newMinut>=60)?1:0);
			newMinut = (newMinut>=60)?newMinut-60:newMinut;
			newHour = (newHour>=240)?newHour-24:newHour;
		}

		this.alarmTime.setHours(newHour);
		this.alarmTime.setMinutes(newMinut);
	}

	  static private String hexEncode( byte[] aInput){
		    StringBuilder result = new StringBuilder();
		    char[] digits = {'0', '1', '2', '3', '4','5','6','7','8','9','a','b','c','d','e','f'};
		    for ( int idx = 0; idx < aInput.length; ++idx) {
		      byte b = aInput[idx];
		      result.append( digits[ (b&0xf0) >> 4 ] );
		      result.append( digits[ b&0x0f] );
		    }
		    return result.toString();
		  }	
	
	
}

